package com.example.user.myhjjf_app.view;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.example.user.myhjjf_app.HJJF_Url;
import com.example.user.myhjjf_app.R;
import com.example.user.myhjjf_app.model.Model_Oc;
import com.example.user.myhjjf_app.model.Model_Tagging;
import com.example.user.myhjjf_app.model.Model_guanglan;
import com.example.user.myhjjf_app.model.Model_rowCol;
import com.example.user.myhjjf_app.presenter.Presenter_GjxXq_adapter;
import com.example.user.myhjjf_app.presenter.Presenter_Glitem_adapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.adapter.abslistview.MultiItemTypeAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Activity_GjxXq extends AppCompatActivity {
    Model_Tagging.ValueBean.ArrayBean.DetailsBean detailsBean;
    Model_guanglan guanglan;
    Model_Oc oc;
    MultiItemTypeAdapter adapter;
    List<Model_guanglan.ValueBean> lsit = new ArrayList<>();
    List<String> ocName = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    List<String> rc = new ArrayList<>();
    List<String> ywType = new ArrayList<>();
    Map<String, Model_rowCol> nums = new HashMap<>();
    Map<String, Model_guanglan.ValueBean> key = new HashMap<>();
    List<String> Ids = new ArrayList<>();
    MultiItemTypeAdapter adapter2;
    int i;

    TextView txGlmc, txYwlx, txYwmc, txYd,txto,txname;
    EditText txMc,edMc;
    Spinner spTo,spGl,spType;

    @BindView(R.id.Im_back)
    LinearLayout ImBack;
    @BindView(R.id.tx_title)
    TextView txTitle;
    @BindView(R.id.gridView)
    GridView gridView;
    @BindView(R.id.bt_tq)
    Button btTq;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__gjx_xq);
        ButterKnife.bind(this);
        final Bundle bundle = getIntent().getExtras();
        detailsBean = (Model_Tagging.ValueBean.ArrayBean.DetailsBean) bundle.getSerializable("detailsBean");
        init();
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int row = i / 12 + 1;
                int col = i % 12 + 1;
                String k = row + "-" + col;
                if (key.containsKey(k)) {
                    Model_guanglan.ValueBean valueBean = key.get(k);
                    if (valueBean.getFrom_() != null) {
                        seletcZy(valueBean);
                    } else {
                        selectGl(valueBean, i);
                    }
                } else {
                    Model_rowCol col1 = new Model_rowCol(row, col);

                    if (nums.containsKey(k)) {

                        nums.remove(k);
                    } else {
                        nums.put(k, col1);
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });

    }

    private void init() {
        nums.clear();
        list.clear();
        key.clear();
        rc.clear();
        Ids.clear();
        ocName.clear();
        ywType.clear();
        ywType.add("家客");
        ywType.add("集客");
        ywType.add("基站");
        ywType.add("组网");
            OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/site/gjx_port_list.do"))
                    .tag(this)
                    .params("id", detailsBean.getId())
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            guanglan = JSON.parseObject(response.body(), Model_guanglan.class);
                            for (int i = 0; i < 48 * 12; i++) {
                                list.add(i);
                            }
                            for (int i = 0; i < guanglan.getValue().size(); i++) {
                                lsit.add(guanglan.getValue().get(i));
                                String k = guanglan.getValue().get(i).getRow_() + "-" + guanglan.getValue().get(i).getCol_();
                                key.put(k, guanglan.getValue().get(i));
                                if (guanglan.getValue().get(i).getFrom_() == null) {
                                    rc.add(guanglan.getValue().get(i).getRow_() + "行" + guanglan.getValue().get(i).getCol_() + "列");
                                    Ids.add(guanglan.getValue().get(i).getId());
                                }
                            }
                            adapter.notifyDataSetChanged();
                        }
                    });
            adapter = new MultiItemTypeAdapter(this, list);
            adapter.addItemViewDelegate(new Presenter_GjxXq_adapter(key, nums));
            gridView.setAdapter(adapter);

            OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/site/oc_list.do"))
                    .tag(this)
                    .params("id",detailsBean.getId())
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            oc = JSON.parseObject(response.body(), Model_Oc.class);
                            for (int i = 0; i < oc.getValue().size(); i++) {
                                ocName.add(oc.getValue().get(i).getName());
                            }
                        }
                    });
        }


    private void seletcZy(final Model_guanglan.ValueBean valueBean){
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.activity_gladd_selectzy, null);
        txGlmc = view.findViewById(R.id.tx_glmc);
        txYwlx = view.findViewById(R.id.tx_ywlx);
        txYwmc = view.findViewById(R.id.tx_ywmc);
        txname = view.findViewById(R.id.tx_name);
        txto = view.findViewById(R.id.tx_to);
        txYd = view.findViewById(R.id.tx_yd);

        txGlmc.setText(valueBean.getOc_name());
        txYwmc.setText((String) valueBean.getB_name());
        txYwlx.setText((String) valueBean.getB_type());
        txname.setText((String)valueBean.getName());
        txto.setText((String)valueBean.getTo_());
        txYd.setText((String)valueBean.getFrom_());
        AlertDialog ss = new AlertDialog.Builder(this).setTitle("占用").setView(view)
                .setPositiveButton("确定", null).show();
    }



    private void addgl(){
        String name = edMc.getText().toString();
        i=0;
        if (!name.equals("")){
            String oc = ocName.get(spGl.getSelectedItemPosition());
            String type = ywType.get(spType.getSelectedItemPosition());
            Model_rowCol model = null;
            for (Model_rowCol model_rowCol : nums.values()){
                model = model_rowCol;
                i++;
                OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/data/gjxPort_add.do"))
                        .tag(this)
                        .params("gjxId",detailsBean.getId())
                        .params("bName",name)
                        .params("bType",type)
                        .params("ocName",oc)
                        .params("rows",model.getRow())
                        .params("cols",model.getCol())
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                if (i==nums.size()){
                                    init();
                                }
                            }
                        });
            }
        }
    }

    private void addselect(){
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.activity_gladd,null);
        spGl = view.findViewById(R.id.sp_gl);
        spType = view.findViewById(R.id.sp_type);
        edMc = view.findViewById(R.id.ed_mc);

        adapter2 = new MultiItemTypeAdapter(this, ocName);
        adapter2.addItemViewDelegate(new Presenter_Glitem_adapter());
        spGl.setAdapter(adapter2);

        adapter2 = new MultiItemTypeAdapter(this,ywType);
        adapter2.addItemViewDelegate(new Presenter_Glitem_adapter());
        spType.setAdapter(adapter2);

        AlertDialog dialog = new AlertDialog.Builder(this).setTitle("关联光缆").setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        addgl();
                    }
                })
                .setNegativeButton("取消", null)
                .show();



    }

    private void selectGl(final Model_guanglan.ValueBean valueBean, int position) {
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.activity_gladd_zy, null);
        txGlmc = view.findViewById(R.id.tx_glmc);
        txYwlx = view.findViewById(R.id.tx_ywlx);
        txYwmc = view.findViewById(R.id.tx_ywmc);
        txMc = view.findViewById(R.id.tx_mc);
        txYd = view.findViewById(R.id.tx_yd);
        spTo = view.findViewById(R.id.sp_to);


        txGlmc.setText(valueBean.getOc_name());
        txYwmc.setText((String) valueBean.getB_name());
        txYwlx.setText((String) valueBean.getB_type());
        int row = position / 12 + 1;
        int col = position % 12 + 1;
        final String from = row + "行" + col + "列";
        txYd.setText(from);
        adapter2 = new MultiItemTypeAdapter(this, rc);
        adapter2.addItemViewDelegate(new Presenter_Glitem_adapter());
        spTo.setAdapter(adapter2);
        AlertDialog jum = new AlertDialog.Builder(this).setTitle("跳纤").setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        tqqd(valueBean, from);
                    }
                })
                .setNegativeButton("取消", null)
                .setNeutralButton("删除关联", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/data/gjxPort_drop.do"))
                                .tag(this)
                                .params("id", valueBean.getId())
                                .execute(new StringCallback() {
                                    @Override
                                    public void onSuccess(Response<String> response) {
                                        init();
                                    }
                                });
                    }
                })
                .show();
    }


    @OnClick({R.id.Im_back, R.id.bt_tq})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Im_back:
                finish();
                break;
            case R.id.bt_tq:
                if (nums.size()==0){
                    Toast.makeText(this, "请至少选中一个端口~~", Toast.LENGTH_SHORT).show();
                }else {
                    addselect();
                }
                break;
        }
    }


    private void tqqd(Model_guanglan.ValueBean valueBean, String from) {
        String name = txMc.getText().toString();
        String to = rc.get(spTo.getSelectedItemPosition());
        String toid = Ids.get(spTo.getSelectedItemPosition());
        if (name.equals("")) {
            Toast.makeText(this, "名称不能为空~", Toast.LENGTH_SHORT).show();
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("from", valueBean.getId());
            map.put("to", toid);
            map.put("name", name);
            map.put("from_", from);
            map.put("to_", to);
            OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/data/gjxPort_jump.do"))
                    .tag(this)
                    .params(map)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            init();
                        }
                    });

        }
    }

}
